package com.loan.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.loan.common.annotation.Log;
import com.loan.common.constant.UserConstants;
import com.loan.common.core.controller.BaseController;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.domain.Result;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.enums.BusinessType;
import com.loan.system.domain.CompanyEntity;
import com.loan.system.domain.SmsConfigEntity;
import com.loan.system.domain.SysConfig;
import com.loan.system.domain.req.CompanyReq;
import com.loan.system.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shiguang on 2022/9/5 10:47
 */
@Validated
@Api(value = "平台控制器", tags = {"平台控制器"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/company")
public class CompanyController extends BaseController {

    private final CompanyService companyService;

    /**
     * 获取公司列表
     */
    @ApiOperation("获取公司列表(分页)")
    //@SaCheckPermissio("system:company:list")
    @GetMapping("/page")
    public TableDataInfo<CompanyEntity> page(CompanyEntity entity, PageQuery pageQuery) {
        return companyService.selectPageCompanyList(entity, pageQuery);
    }

    /**
     * 获取公司列表
     */
    @ApiOperation("获取公司列表")
    //@SaCheckPermissio("system:company:list")
    @GetMapping("/list")
    public Result<List<CompanyEntity>> list() {
        return Result.ok(companyService.selectList());
    }

//    /**
//     * 新增公司
//     */
//    @ApiOperation("新增公司")
//    //@SaCheckPermissio("system:company:add")
//    @Log(title = "新增公司", businessType = BusinessType.INSERT)
//    @PostMapping
//    public Result<Void> add(@Validated @RequestBody CompanyReq company) {
//        if (UserConstants.NOT_UNIQUE.equals(companyService.checkNameUnique(company))) {
//            return Result.fail("新增'" + company.getName() + "'失败，名称已存在");
//        }
//        return toAjax(companyService.insertCompany(company));
//    }

    /**
     * 根据id查询详情
     */
    @ApiOperation("根据id查询详情")
    //@SaCheckPermissio("system:company:query")
    @GetMapping(value = {"/{id}"})
    public Result<CompanyEntity> getInfo(@ApiParam("ID") @PathVariable(value = "id", required = false) Long id) {
        if (ObjectUtil.isNotNull(id)) {
            return Result.ok(companyService.selectCompanyById(id));
        }
        return Result.ok();
    }


    @ApiOperation("根据用户ID获取公司详情")
    @GetMapping("/getDetail")
    public Result<CompanyEntity> getDetail() {
            return Result.ok(companyService.getDetail());
    }

    /**
     * 新增公司
     */
    @ApiOperation("新增公司")
    //@SaCheckPermissio("system:company:add")
    @Log(title = "新增公司", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Void> add(@Validated @RequestBody CompanyEntity entity) {
        return toAjax(companyService.addCompany(entity));
    }

    /**
     * 修改公司
     */
    @ApiOperation("修改公司")
    //@SaCheckPermissio("system:company:update")
    @Log(title = "修改公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Void> update(@Validated @RequestBody CompanyEntity entity) {
        return toAjax(companyService.updateCompany(entity));
    }

    /**
     * 删除公司
     */
    @ApiOperation("删除公司")
    //@SaCheckPermissio("system:company:remove")
    @Log(title = "删除公司", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result<Void> remove(@ApiParam("ID") @PathVariable("id") Long id) {
        return toAjax(companyService.deleteCompanyById(id));
    }

    /**
     * 状态修改
     */
    @ApiOperation("状态修改")
    //@SaCheckPermissio("system:company:edit")
    @Log(title = "状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public Result<Void> changeStatus(@RequestBody CompanyEntity entity) {
        return toAjax(companyService.updateStatus(entity));
    }


    /**
     * 还款方式修改
     */
    @ApiOperation("还款方式修改")
    //@SaCheckPermissio("system:company:edit")
    @Log(title = "还款方式修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changePaymentTypeStatus")
    public Result<Void> changePaymentTypeStatus(@RequestBody CompanyEntity entity) {
        return toAjax(companyService.updateStatus(entity));
    }

    /**
     * 打款方式修改
     */
    @ApiOperation("打款方式修改")
    //@SaCheckPermissio("system:company:edit")
    @Log(title = "打款方式修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changePayTypeStatus")
    public Result<Void> changePayTypeStatus(@RequestBody CompanyEntity entity) {
        return toAjax(companyService.updateStatus(entity));
    }


    @ApiOperation("公债明细平台名称显示")
    @Log(title = "公债明细平台名称显示", businessType = BusinessType.UPDATE)
    @PutMapping("/changePublicOpen")
    public Result<Void> changePublicOpen(@RequestBody CompanyEntity entity) {
        return toAjax(companyService.updateStatus(entity));
    }
}
